home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2003 June / macformat-130.iso / mac / Reviewed⁄Demos / Spearhead Demo / demota / pak1.pk3 / global / vehicle_warning.scr < prev    next >
Encoding:
Text File  |  2002-10-21  |  4.8 KB  |  194 lines

  1. //--------------------------------------------------------------------------------
  2. //    Vehicle_Warning.Scr
  3. //    Jeff Leggett
  4. //    07/12/2002
  5. //
  6. //        Use this script to have your vehicles warn AI characters that they are
  7. //    coming.  The AI character will then run to the closest "vehicle_safety_node".
  8. //
  9. //    How to use:
  10. //        1.    info_pathnodes that are "safe" (ie: AI will be out of the way of 
  11. //            the vehicle) should be given a targetname of vehicle_safety_node
  12. //        2.    create the following thread when the vehicle starts moving:
  13. //
  14. //                $tankname thread global/Vehicle_Warning.Scr::WarnFriendlies $ai_name <radius=800> <cos_angle=0.707>
  15. //
  16. //        where $tankname is the name of your vehicle, $ai_name is the name of the 
  17. //        ai(s) that you want to potentially warn, radius is the distance to warn at
  18. //        cos_angle is the cosine of the angle you want to have the vehicle warn at (0.707=cos(45)) 
  19. //        (defaults to 45 degrees)
  20. //
  21. //    Example usage:
  22. //
  23. //
  24. //        // start up the warning...
  25. //        $tank thread global/Vehicle_Warning.Scr::WarnFriendlies $german_scum 800 0.707
  26. //        $tank.german_scum_thread = parm.previousthread
  27. //        $tank drive $tank_path 200 30 200 256
  28. //        $tank waittill drive
  29. //        // now that the tank has stopped moving, cancel the warnings...(delete the thread.)
  30. //        $tank.german_scum_thread delete
  31. //        
  32. //--------------------------------------------------------------------------------
  33.  
  34.  
  35. //--------------------------------------------------------------------------------
  36. main:
  37. //
  38. //    Just in case...
  39. //
  40. //--------------------------------------------------------------------------------
  41.  
  42.     End
  43.  
  44. //------------------------------------------------------------------------------
  45. CanSeeTarget local.target local.radius local.angle local.reverse:
  46. //
  47. //
  48. //    example:
  49. //        local.bCanSee = self waitthread CanSeeTarget $mytarget 500 0.707
  50. //
  51. //            if ( local.bCanSee==1 )
  52. //                dprintln "I can see!"
  53. //
  54. //------------------------------------------------------------------------------
  55.  
  56.     if ( local.target==NIL )
  57.         End 0
  58.  
  59.     local.dist = vector_length (self.origin - local.target.origin)
  60.  
  61.     if ( local.dist > local.radius )
  62.     {
  63.         End 0
  64.     }
  65.  
  66.     if ( local.angle==NIL )
  67.     {
  68.         End 1
  69.     }
  70.  
  71.     if ( local.reverse==NIL )
  72.         local.reverse = 0
  73.  
  74.  
  75.     local.vObjDir    = vector_subtract local.target.origin self.origin
  76.     local.vObjDir    = vector_normalize local.vObjDir
  77.     local.vForward  = angles_toforward ( self.angles )
  78.  
  79.     local.dot        = vector_dot local.vForward local.vObjDir
  80.  
  81.     // this is for seeing behind the object. (used for trucks with guys on the back of them.)
  82.     if ( local.reverse==1 )
  83.     {
  84.         if ( local.dot <= local.angle )
  85.         {
  86.             End 1
  87.         }
  88.     }
  89.     else
  90.     {
  91.         if ( local.dot >= local.angle )
  92.         {
  93.             End 1
  94.         }
  95.     }
  96.  
  97.     End 0
  98.  
  99. //--------------------------------------------------------------------------------
  100. WarnFriendlies local.name local.radius local.angle:
  101. //
  102. //
  103. //    start this thread to have the vehicle start warning...
  104. //
  105. //--------------------------------------------------------------------------------
  106.  
  107.     if ( local.name==NIL )
  108.         End
  109.  
  110.     if (local.name.size<1 )
  111.         end
  112.  
  113.     if ( local.radius==NIL )
  114.         local.radius    = 800
  115.  
  116.     if ( local.angle==NIL )
  117.         local.angle    = 0.707
  118.  
  119.     if ( self.paused==NIL )
  120.         self.paused = 0
  121.  
  122.     while ( isAlive self )
  123.     {
  124.         if ( self.paused==0 )
  125.         {
  126.             for (local.i=1;local.i<=local.name.size;local.i++)
  127.             {
  128.                 if ( local.name[local.i].bVehicleWarned==0 || local.name[local.i].bVehicleWarned==NIL )
  129.                 {
  130.                     local.bShouldWarn = self waitthread CanSeeTarget local.name[local.i] local.radius local.angle
  131.                     if ( local.bShouldWarn==1 )
  132.                     {
  133.                         local.name[local.i] thread RunToSafety
  134.                     }
  135.                 }
  136.  
  137.             }
  138.             wait 0.25
  139.         }
  140.         else
  141.         {
  142.             wait 1
  143.         }
  144.     }
  145.  
  146.     End
  147.  
  148.  
  149. //--------------------------------------------------------------------------------
  150. RunToSafety:
  151. //
  152. //
  153. //    Find closest safety node and run to it...
  154. //
  155. //--------------------------------------------------------------------------------
  156.  
  157.     if ( $vehicle_safety_node==NIL || $vehicle_safety_node.size < 1 )
  158.     {
  159.         dprintln "No safety nodes have been created!"
  160.         end
  161.     }
  162.  
  163.     if ( self.bVehicleWarned == 1 )
  164.         End
  165.  
  166.     self.bVehicleWarned    = 1
  167.  
  168.     local.closest = $vehicle_safety_node[1]
  169.     local.closestdist = vector_length (self.origin - $vehicle_safety_node[1].origin)
  170.  
  171.     for (local.i=2;local.i<=$vehicle_safety_node.size;local.i++)
  172.     {
  173.         local.dist = vector_length (self.origin - $vehicle_safety_node[local.i].origin)
  174.  
  175.         if ( local.dist < local.closestdist )
  176.         {
  177.             local.closest    = $vehicle_safety_node[local.i]
  178.             local.closestdist = local.dist
  179.         }
  180.     }
  181.  
  182.     // make sure we're not already there...
  183.     if ( local.closestdist > 50 )
  184.     {
  185.         self exec global/disable_ai.scr
  186.         self runto local.closest
  187.         self waittill movedone
  188.         self exec global/enable_ai.scr
  189.     }
  190.  
  191.     self.bVehicleWarned    = 0
  192.  
  193.     End
  194.